'''

    Playing with the state machine.

'''

from PyQt4 import QtGui, QtCore
import sys

class StateEvent(QtCore.QEvent):
    def __init__(self, val):
        QtCore.QEvent.__init__(self, QtCore.QEvent.User+1)
        self.value = val
 
class StateTransition(QtCore.QAbstractTransition):
    def __init__(self, value):
        QtCore.QAbstractTransition.__init__(self)
        self.value = value
        
    def eventTest(self, event):
        if event.type() != QtCore.QEvent.Type(QtCore.QEvent.User+1):
            return False
        return self.value in event.value

    def onTransition(self, event):
        pass

class StateLabel(QtGui.QLabel):
    def __init__(self, parent, parentState):
        QtGui.QLabel.__init__(self, parent)
        self.setLineWidth(5)
        self.setFrameShape(QtGui.QFrame.Panel|QtGui.QFrame.Sunken)
        self.setAlignment(QtCore.Qt.AlignHCenter|QtCore.Qt.AlignVCenter)
        self.setAutoFillBackground(True)
        defaultPalette = QtGui.QPalette(self.palette)
        
        self.parallelState = QtCore.QState(parentState)
        
        self.stateA = stateA = QtCore.QState(self.parallelState)
#        stateA.assignProperty(
#            self, "frameShape", QtGui.QFrame.WinPanel,
#        )
        stateA.assignProperty(
            self, "lineWidth", 0,
        )
#        stateA.assignProperty(
#            self, "alignment", QtCore.Qt.AlignLeft|QtCore.Qt.AlignVCenter,
#        )
        stateA.assignProperty(
            self, "palette", defaultPalette,
        )

        self.stateB = stateB = QtCore.QState(self.parallelState)
#        stateB.assignProperty(
#            self, "frameShape", QtGui.QFrame.Panel|QtGui.QFrame.Sunken,
#        )
        stateB.assignProperty(
            self, "lineWidth", 3,
        )
#        stateB.assignProperty(
#            self, "alignment", QtCore.Qt.AlignRight|QtCore.Qt.AlignVCenter,
#        )
        stateB.assignProperty(
            self, "palette", QtGui.QPalette(QtGui.QColor(250,250,200)),
        )

        toStateB = StateTransition("B")
        toStateB.setTargetState(stateB)
        stateA.addTransition(toStateB)
        
        toStateA = StateTransition("A")
        toStateA.setTargetState(stateA)
        stateB.addTransition(toStateA)
        
        self.parallelState.setInitialState(stateA)
        
class Widget(QtGui.QWidget):
    def __init__(self, parent):
        QtGui.QWidget.__init__(self, parent)
        
        self.stateMachine = QtCore.QStateMachine(self)
        self.parentState = QtCore.QState(QtCore.QState.ParallelStates)
        self.stateMachine.addState(self.parentState)
        
        self.buttA = QtGui.QPushButton("Butt A", self)
        self.connect(
            self.buttA, QtCore.SIGNAL( "clicked()"), self.OnButtA
        )
        
        self.buttB = QtGui.QPushButton("Butt B", self)
        self.connect(
            self.buttB, QtCore.SIGNAL( "clicked()"), self.OnButtB
        )
        
        self.lbA = StateLabel(self, self.parentState)
        self.lbB = StateLabel(self, self.parentState)
        self.lbC = StateLabel(self, self.parentState)
        
        HL = QtGui.QHBoxLayout()
        HL.addWidget(self.buttA)
        HL.addWidget(self.buttB)
        HL.addWidget(self.lbA)
        HL.addWidget(self.lbB)
        HL.addWidget(self.lbC)
        self.setLayout(HL)

        self.stateMachine.setInitialState(self.parentState)
        self.stateMachine.start()
    
#    def setup_states(self):
#        lb = self.lbA
#        
#        self.stateA = stateA = QtCore.QState()
#        stateA.assignProperty(
#            lb, "alignment", QtCore.Qt.AlignLeft,
#        )
#        self.stateB = stateB = QtCore.QState()
#        stateB.assignProperty(
#            lb, "alignment", QtCore.Qt.AlignRight,
#        )
#        #stateA.addTransition(self.buttA, QtCore.SIGNAL("clicked()"), stateB);
#        #stateB.addTransition(self.buttA, QtCore.SIGNAL("clicked()"), stateA);
#        #self.trA = stateA.addTransition(stateB)
#        #self.trB = stateB.addTransition(stateA)
#        toStateB = StateTransition("B")
#        toStateB.setTargetState(stateB)
#        stateA.addTransition(toStateB)
#        toStateA = StateTransition("A")
#        toStateA.setTargetState(stateA)
#        stateB.addTransition(toStateA)
#        
#        self.stateMachine.addState(stateA)
#        self.stateMachine.addState(stateB)
#        self.stateMachine.setInitialState(stateA)
#        self.anim = QtCore.QPropertyAnimation(lb, "alignment")
#        #self.stateMachine.addDefaultAnimation(self.anim)
#        self.stateMachine.start()

    def OnButtA(self):
        t = "A Pushed"
        self.lbA.setText('1: '+t)
        self.lbB.setText('2: '+t)
        self.lbC.setText('3: '+t)
        self.stateMachine.postEvent(StateEvent(t))
        
    def OnButtB(self):
        t = "B Pushed"
        self.lbA.setText('1: '+t)
        self.lbB.setText('2: '+t)
        self.lbC.setText('3: '+t)
        self.stateMachine.postEvent(StateEvent(t))
        
class AppWin( QtGui.QMainWindow ):
    def __init__( self, *args ):
        QtGui.QMainWindow.__init__( self, *args )
        
        self.resize( 800, -1 )

        self.center = Widget(self)
        self.setCentralWidget(self.center)

def run():
    app = QtGui.QApplication(sys.argv)

    w = AppWin()
    w.show()
    ret = app.exec_()
    return ret


if __name__ == '__main__':
    sys.exit(run())
    